<script setup>
import { reactive, ref } from 'vue'
import { useUserStore } from "@/stores/user.js"
import { useRouter } from 'vue-router';
const userStore = useUserStore()
const ruleFormRef = ref()
const router = useRouter()
const ruleForm = reactive({
  username: 'admin',
  password: '123456',
})

const rules = reactive({
  username: [
    { required: true, message: '用户名不能为空', trigger: 'blur' },
    { min: 3, max: 5, message: '用户名长度应3到5个字符', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '密码不能为空', trigger: 'blur' },
    { min: 3, max: 6, message: '密码长度应3到6个字符', trigger: 'blur' },
  ],
})

const submitForm = (formEl) => {
  if (!formEl) return
  formEl.validate((valid) => {
    if (valid) {
      //开始用户登录
      userStore.login(ruleForm).then(res => {
        console.log(res);
        if (res.code == 0) {
          router.push("/")
        }
      })
    } else {
      console.log('出现错误!')
      return false
    }
  })
}

</script>
<template>
  <div class="login">
    <el-row class="row-bg" justify="end">
      <el-col :xs="18" :sm="12" :md="10" :lg="8" :xl="6" class="formContainer">
        <el-form ref="ruleFormRef" :model="ruleForm" status-icon :rules="rules" label-width="120px"
          class="demo-ruleForm">
          <el-form-item>
            <h1>欢迎来到米奇妙妙屋</h1>
          </el-form-item>
          <el-form-item label="用户名" prop="username">
            <el-input v-model="ruleForm.username" type="text" />
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="ruleForm.password" type="password" autocomplete="off" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm(ruleFormRef)">提交</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<style lang="less">
.login {
  background-image: url("../assets/img/background.jpg");
  height: 100vh;
  background-position: left center;
  background-size: cover;

  .formContainer {
    height: 100vh;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
  }
}
</style>
